package NWish
{
	import NWish.Interface.IApp;
	
	public class ObjectExplorer
	{
		private var _parent:IApp;
		public function get parent(){return _parent;}
		
		private var _window:Window;
		
		public function ObjectExplorer(parent:IApp)
		{
			//
			_parent = parent;
			
			//
			var menuBar:MenuBar = _parent.getPlugin('MenuBar') as MenuBar;
			menuBar.windowMenu.addMenuItem("&Object List").addActionListener(_showWindow);
			
			//
			_window = new Window(this);
			_window.show();
			
			//
			_parent.rootSprite.addEventListener(NWEvent.CREATE_OBJECT,	_window.onCreateObject);
			_parent.rootSprite.addEventListener(NWEvent.DELETE_OBJECT,	_window.onDeleteObject);
			_parent.rootSprite.addEventListener(NWEvent.CLEAR_SCENE, 	_window.clearList,false,1);
		}
		
		public function init()
		{
			
		}
		
		private function _showWindow(e)
		{
			_window.show();
		}
		
		public function setObjects(objs)
		{
			_window.setObjects(objs);
		}
		
	}
}


import NWish.Misc;
import NWish.NWEvent;
import NWish.ObjectExplorer;

import away3d.cameras.Camera3D;
import away3d.containers.ObjectContainer3D;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import away3d.core.base.Object3D;
import away3d.entities.Entity;
import away3d.primitives.WireframeCube;

import flash.events.MouseEvent;

import org.aswing.*;
import org.aswing.JList;
import org.aswing.border.*;
import org.aswing.colorchooser.*;
import org.aswing.event.AWEvent;
import org.aswing.ext.*;
import org.aswing.geom.*;


class NWListNode
{
	public var object3d;
	public function NWListNode(o)
	{
		object3d = o;
	}
	
	public function toString():String
	{
		var s = object3d.toString() + ' ' + object3d.name;
		
		s = s.replace('object','');
		s = s.replace('[ ','');
		s = s.replace(']',' : ');
		
		if(object3d.hasOwnProperty('visible'))
		{
			if(object3d.visible == false)
				s += "       Hide";
			else
				s += "    Visible";
		}
		
		if(object3d.hasOwnProperty('mouseEnabled'))
		{
			if(object3d.mouseEnabled == false)
				s += "    Lock";
		}

		
		return s;
	}
}

class Window extends JFrame
{
	private var _parent			:ObjectExplorer;
	
	//members define
	private var _list:JList;
	private var _find:JComboBox;
	
	
	public function Window(parent:ObjectExplorer)
	{
		//
		_parent = parent;
		
		//
		super(null,'Object Explorer');
		
		
		//
		setLocationXY(20,40);
		setSize(new IntDimension(265, 640));
		
		//
		var panel:JPanel = new JPanel();
		panel.setSize(new IntDimension(250, 600));
		setContentPane(panel);
		
		//component creation
		var layout0:EmptyLayout = new EmptyLayout();
		panel.setLayout(layout0);
		
		//
		_list = new JList();
		_list.addEventListener(MouseEvent.CLICK,onClick);
		//_list.setLocation(new IntPoint(0, 40));
		//_list.setSize(new IntDimension(248, 650));
		//_list.setListData(['addHere','123456']);
		var sp = new JScrollPane(_list);
		sp.setLocation(new IntPoint(5, 40));
		sp.setSize(new IntDimension(260, 650));
		panel.append(sp);
		
		
		//
		var select:JButton = new JButton('select');
		//var selectAll:JButton = new JButton('select all');
		var multiApply:JButton = new JButton('multi apply');
		//var copyName:JButton = new JButton('copy name');
		var visible:JButton = new JButton('visible');
		var lock:JButton = new JButton('lock');
		var delete_:JButton = new JButton('delete');
		//var properties:JButton = new JButton('    properties    ');
		//var visible:JButton = new JButton('visible');
		//var mouseEnable:JButton = new JButton('mouse enable');
		
		Misc.pack_list([select,multiApply,visible,lock,delete_],0,2);
		
		panel.append(select);
		panel.append(multiApply);
		panel.append(visible);
		panel.append(lock);
		panel.append(delete_);
		
		select.addActionListener(onSelect);
		multiApply.addActionListener(onMultiApply);
		visible.addActionListener(onVisible);
		lock.addActionListener(onLock);
		delete_.addActionListener(onDelete);
		
		
		//
		_find = new JComboBox();
		_find.setLocation(new IntPoint(0, 18));
		_find.setSize(new IntDimension(145, 22));
		_find.setEditable(true);
		_find.setMaximumRowCount(7);
		_find.getEditor().setValue('');
		_find.addActionListener(actionFind);
		panel.append(_find);
		
		var add:JButton = new JButton('add');
		add.setLocation(new IntPoint(147, 18));
		add.setSize(new IntDimension(35, 22));
		add.setToolTipText('add to ComboBox');
		add.addActionListener(actionAdd);
		panel.append(add);
		
		var selectAll:JButton = new JButton('select all');
		selectAll.setLocation(new IntPoint(182, 18));
		selectAll.setSize(new IntDimension(60, 22));
		selectAll.addActionListener(actionSelectAll);
		panel.append(selectAll);
	}
	
	public function get scene()
	{
		var away3d = _parent.parent.getPlugin('Away3d');
		
		if(away3d.view)
			return away3d.view.scene;
		
		return null;
	}
	
	override public function setVisible(v:Boolean):void
	{
		super.setVisible(v);
		
		if(v)
		{
			updateList(null);
		}
	}
	
	public function clearList(e = null)
	{
		_list.setListData(null);
	}
	
	public function updateList(name)
	{
		var arr = [];
		
		if(scene)
			Misc.getChildrenByNameInScene(name,scene,arr);
		
		var arr1 = [];
		for each (var c in arr)
		{
			if(c is WireframeCube == false)
				arr1.push(new NWListNode(c));
		}
		_list.setListData(arr1);
		
		_list.updateListView();
	}

	public function onCreateObject(e:NWEvent)
	{
		if(visible)
		{
			var objs = e.objects;
			if(objs is Array == false)
				objs = [objs];
			
			for each(var o in objs)
			{
				if(o is Object3D)
				{
					var n = _find.getSelectedItem();
					if(o.name.search(n) != -1)
					{
						var vlm = _list.getModel() as VectorListModel;
						vlm.append(new NWListNode(o));
						_list.updateListView();
					}
				}
			}
		}
	}
	
	public function onDeleteObject(e:NWEvent)
	{
		if(visible)
			actionFind(null)
	}
	
	private function _getObject3ds()
	{
		var svs = _list.getSelectedValues();
		var os = [];
		for each(var s in svs)
		{
			if(s)
				os.push(s.object3d);
		}
		return os;
	}
	
	//
	private function onSelect(e)
	{
		if(visible == false)
			return;
			
		if(_list.isSelectionEmpty())
			return;
		
		var os = _getObject3ds();
		if(os.lenght == 1)
			_parent.parent.dispatchEvent(new NWEvent(NWEvent.SELSCT_OBJECT,os[0]));
		else
			_parent.parent.dispatchEvent(new NWEvent(NWEvent.SELSCT_OBJECT,os));
	}
	
	private function onMultiApply(e)
	{
		if(_list.isSelectionEmpty())
			return;
		
		_parent.parent.dispatchEvent(new NWEvent(NWEvent.MULTI_APPLY,_getObject3ds()))
	}
	
	private function onVisible(e)
	{
		if(_list.isSelectionEmpty())
			return;
		
		var os = _getObject3ds();
		
		for each(var obj:Object in os)
		{
			if(obj.hasOwnProperty('visible'))
				obj.visible = !obj.visible;
		}
		
		_list.updateUI();
		//_list.updateListView();
	}
	
	private function onLock(e)
	{
		if(_list.isSelectionEmpty())
			return;
		
		var os = _getObject3ds();

		for each(var obj in os)
		{
			if(obj is Entity)
				obj.mouseEnabled = !obj.mouseEnabled;
		}
		
		_list.updateUI();
		//_list.updateListView();
	}
	
	private function onDelete(e)
	{
		if(_list.isSelectionEmpty())
			return;
		
		_parent.parent.dispatchEvent(new NWEvent(NWEvent.DELETE_OBJECT,_getObject3ds()))
	}
	
	private function actionFind(e)
	{
		//trace(_find.getSelectedItem());
		updateList(_find.getSelectedItem());
	}
	private function actionAdd(e)
	{
		if(_find.getEditor().getValue())
			(_find.getModel() as VectorListModel).append(_find.getEditor().getValue(),0);
	}
	
	private function actionSelectAll(e)
	{
		_list.selectAll();
		//_list.updateUI();
		_list.updateListView();
	}
	
	private var _clickObject;
	private function onClick(e)
	{
		if(_list.isSelectionEmpty())
			return;
		
		var os = _getObject3ds();
		
		if(os.length == 1)
		{
			var o = os[0];
			if(o && _clickObject == o)
				_parent.parent.getPlugin('CameraHDController').target = o.position;
			
			_clickObject = o;
		}
		else
		{
			_clickObject = null;
		}
	}
	
	
	public function setObjects(objs)
	{
		var lns = [];
		for each(var o in objs)
			lns.push(new NWListNode(o));
			
		_list.setListData(lns);
		
		//_list.updateUI();
		_list.updateListView();
	}
	
}








/*
private function onCopyName(e)
{
	if(_list.isSelectionEmpty())
		return;
	
	var os = _getObject3ds();
	var str = '';
	
	for each(var obj in os)
	{
		if(str.length)
			str += ', ';
		
		str += obj.name;
	}
	
	Clipboard.generalClipboard.clear();
	Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, str);
}*/





